package io.nuls.klines.api;

import lombok.extern.slf4j.Slf4j;
import io.nuls.klines.api.response.ResponseData;
import io.nuls.klines.entity.KlinesEntity;
import io.nuls.klines.service.AppService;
import io.nuls.klines.util.TimeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: PierreLuo
 * @date: 2020-04-14
 */
@RestController
@RequestMapping("/klines")
@Slf4j
public class KlinesApi {

    @Resource
    private AppService appService;

    @GetMapping("/today")
    public ResponseData getKLinesOfToday() {
        try {
            String today = TimeUtil.todayDateFormat();
            List<KlinesEntity> list = appService.getKlineList(today);
            return ResponseData.success().setData(list);
        } catch (Exception e) {
            log.error("system error", e);
            return ResponseData.error(e.getMessage());
        }
    }

    @GetMapping("/{day}")
    public ResponseData getKLinesByDay(@PathVariable String day) {
        try {
            List<KlinesEntity> list = appService.getKlineList(day);
            return ResponseData.success().setData(list);
        } catch (Exception e) {
            log.error("system error", e);
            return ResponseData.error(e.getMessage());
        }
    }

}
